package com.ruoyi.web.controller.function;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.entity.Facility;
import com.ruoyi.system.service.FacilityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Y·C
 * @version 1.0.0
 * @ClassName FacilityController.java
 * @Description TODO
 * @createTime 2023年12月19日 21:40:00
 */
@Api(tags = "设备管理接口")
@RestController
@RequestMapping("/facility")
public class FacilityController extends BaseController {

    @Autowired
    private FacilityService facilityService;


    @Anonymous
    @ApiOperation(value = "查询设备列表")
    @GetMapping("/getFacilityList")
    public TableDataInfo getFacilityList(Facility facility) {
        startPage();
       List<Facility> facilities = facilityService.getFacilityList(facility);
        return getDataTable(facilities);
    }

    @Anonymous
    @ApiOperation(value = "根据id获取设备信息")
    @GetMapping("/getFacilityById/{id}")
    public Facility getFacilityById(Long id) {
        return facilityService.getById(id);
    }

    @Anonymous
    @ApiOperation("删除多条设备信息")
    @PostMapping("/deleteFacilityByIds")
    public boolean deleteFacilityByIds(@RequestBody List<Long> ids) {

        LambdaQueryWrapper<Facility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityLambdaQueryWrapper.in(Facility::getId, ids);
        List<Facility> list = facilityService.list(facilityLambdaQueryWrapper);
        //判断list 状态是否为0 如果为0则不允许删除
        list.stream().forEach(facility -> {
            if (facility.getStatus() == 0) {
                throw new RuntimeException("设备正在绑定状态，不允许删除，请解除绑定后再删除");
            }
        });
        return facilityService.removeByIds(ids);
    }

    @Anonymous
    @ApiOperation("新增设备信息")
    @PostMapping("/addFacility")
    public boolean addFacility(@RequestBody Facility facility) {
        return facilityService.save(facility);
    }

    @Anonymous
    @ApiOperation("修改设备信息")
    @PostMapping("/updateFacility")
    public boolean updateFacility(@RequestBody Facility facility) {
        return facilityService.updateById(facility);
    }

}
